﻿namespace Gaiaware.Web.SkinSandbox.Utilities
{
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.IO;
    using System.Web.Caching;

    public class WebUtility
    {
        private const string DefaultTheme = "darkblue";

        public static string[] GetThemes()
        {
            //if already cached
            if (HttpContext.Current.Cache["SiteThemes"] != null)
                return (string[])HttpContext.Current.Cache["SiteThemes"];

            string themesDirPath = HttpContext.Current.Server.MapPath("~/App_Themes");

            string[] themeDirs = Directory.GetDirectories(themesDirPath);

            List<string> themeNames = new List<string>();

            for (int i = 0; i <= themeDirs.Length - 1; i++)
            {
                string themeFileName = Path.GetFileName(themeDirs[i].ToLower());
                if (!themeFileName.StartsWith("."))
                    themeNames.Add(themeFileName);
            }

            // cache the array
            CacheDependency dep = new CacheDependency(themesDirPath);
            HttpContext.Current.Cache.Insert("SiteThemes", themeNames.ToArray(), dep);

            return themeNames.ToArray();
        }

        public static String StyleSheetTheme
        {
            get
            {
                //set default theme
                if (HttpContext.Current.Session["CurrentTheme"] == null)
                    StyleSheetTheme = DefaultTheme;

                return HttpContext.Current.Session["CurrentTheme"].ToString();

            }
            set { HttpContext.Current.Session["CurrentTheme"] = value; }
        }
    }
}
